package com.euond.accv.mdm.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.euond.accv.mdm.domain.MdmStoreGroup;
import com.euond.accv.mdm.dto.MdmStoreGroupDTO;
import com.euond.accv.mdm.service.MdmStoreGroupService;
import com.euond.common.domain.search.SearchOperator;
import com.euond.common.domain.search.Searchable;
import com.euond.common.vo.ResultVO;
import com.euond.common.web.controller.BaseSimplePageController;

@Controller
@RequestMapping(value = "/mdm_store_group")
public class MdmStoreGroupController extends BaseSimplePageController<MdmStoreGroup, Integer>{
	@Autowired
	public MdmStoreGroupService service;
	
	@Override
	protected void init() {
		setBaseService(service);
		setFtlPath("/ftl/mdm/mdmStoreGroup/mdmStoreGroup");
	}
	
	/**
	 * 
	* @Title: findStoreGrop 
	* @Description: TODO(初始化第一级节点) 
	* @param @param req
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	@RequestMapping("/findGrop.json")
    @ResponseBody
	public Map<String,Object> findStoreGrop(HttpServletRequest req){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		ResultVO flag = new ResultVO();
		//优化加载速度只展现第一级,其他的点击加载
		Searchable searchable =  Searchable.newSearchable().
				addSort(new Sort(Sort.Direction.DESC, "pStoreGroupNo"));
		 List<MdmStoreGroupDTO> list = new ArrayList<MdmStoreGroupDTO>();
		 searchable.addSearchFilter("pStoreGroupNo", SearchOperator.eq, "1");
		 list= service.findMdmGrop(searchable);    
        // 结果集封装
        resultMap.put("data", list);
        resultMap.put("flag", flag);
        return resultMap;
	}
	/**
	 * 
	* @Title: findStoreGrop 
	* @Description: TODO(加载节点下面的数据) 
	* @param @param req
	* @param @param id
	* @param @param isLeaf
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	@RequestMapping("/findTreeById.json")
    @ResponseBody
	public Map<String,Object> findStoreGropById(HttpServletRequest req,@RequestParam String storeGroupNo,@RequestParam Integer isLeaf){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		ResultVO flag = new ResultVO();
		//优化加载速度只展现第一级,其他的点击加载
		Searchable searchable =  Searchable.newSearchable().
				addSort(new Sort(Sort.Direction.DESC, "pStoreGroupNo"));
		 List<MdmStoreGroupDTO> list = new ArrayList<MdmStoreGroupDTO>();
		 searchable.addSearchFilter("pStoreGroupNo", SearchOperator.eq, storeGroupNo);
		 list= service.findMdmGropById(searchable,isLeaf);
	
        // 结果集封装
        resultMap.put("data", list);
        resultMap.put("flag", flag);
        return resultMap;
	}
	
	/**
	 * 
	* @Title: findStoreGropById 
	* @Description: TODO(加载网格) 
	* @param @param searchable
	* @param @param req
	* @param @param model
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	@RequestMapping("/findGropById.json")
    @ResponseBody
	public Map<String,Object> findStoreGropById(Searchable searchable,HttpServletRequest req,
			Model model){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		ResultVO flag = new ResultVO();
		//需要ID转换成pStoreGroupNo
		
		String storeGroupNo= req.getParameter("storeGroupNo");//searchable.getValue("id_eq").toString();
		searchable.removeSearchFilter("id_eq");
		searchable.addSearchParam("pStoreGroupNo_eq", storeGroupNo);
		searchable.addSort(new Sort(Sort.Direction.ASC, "orderNo"));
        Page<MdmStoreGroup> page = service.findAll(searchable);
        // 结果集封装
		resultMap.put("flag", flag);
		resultMap.put("rows", page.getContent());
		resultMap.put("total", page.getTotalElements());
        return resultMap;
	}
	
}